#!/usr/bin/bash
set -e

package_path="/srv/package/miniconda.sh"
install_dir="/srv/software/miniconda"
custom_env_file="/etc/profile.d/custom_env.sh"

# 创建存放目录，下载和安装
mkdir -p /srv/package /srv/software
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ${package_path}
bash ${package_path} -b -p ${install_dir}

# 配置环境变量
cat >> ${custom_env_file} <<-EOF
export MINICONDA_HOME=${install_dir}
export PATH=\$MINICONDA_HOME/bin:\$PATH
EOF
source /etc/profile

# 配置清华 Conda 镜像源
cat > ${HOME}/.condarc <<-'EOF'
channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
EOF
conda clean -i -y

# 安装成功提示
echo "Successfully installed miniconda..."
echo "Successfully Change Tsinghua source..."
echo "Conda version: `conda -V`"
